package pl.wroc.uni.ii.evolution.engine.operators.spacespecific.binaryvector.ecga;

import java.util.ArrayList;

/**
 * A structure for ECGA algorithm. It contains a partition of chromosome indexes into blocks.
 * 
 * @author Marcin Golebiowski
 */
public class EvECGAStructure {

  /**
   * contains blocks 
   */
  private ArrayList<EvBlock> blocks;

  /**
   * default constructor
   */
  public EvECGAStructure() {
    blocks = new ArrayList<EvBlock>();
  }

  /**
   * add <code>some</code> into Structure
   */
  public void addBlock( EvBlock some ) {
    blocks.add( some );
  }

  /**
   * Remove from structure block <code> first </code> and <code> second </code>.
   * 
   * @param first
   * @param second
   */
  public void remove( EvBlock first, EvBlock second ) {
    for( int i = blocks.size() - 1; i >= 0; i-- ) {
      if ( blocks.get( i ) == first || blocks.get( i ) == second ) {
        blocks.remove( i );
      }
    }
  }

  /**
   * Gets set size
   * @return set size
   */
  public int getSetCount() {
    return blocks.size();
  }

  /**
   * Gets structure rating
   * @return rating
   */
  public double getRating() {
    double sum = 0.0;

    for( EvBlock b : blocks ) {
      sum += b.getRating();
    }
    return sum;
  }

  /**
   * Gets the i'th block
   * @param i index of block to get
   * @return i'th block
   */
  public EvBlock getBlock( int i ) {
    return blocks.get( i );
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();

    for( EvBlock block : blocks ) {
      sb.append( block.toString() );
      sb.append( " " );
    }
    return sb.toString();
  }

}
